<?php
/**
 * Created by PhpStorm.
 * User: Admin
 * Date: 2020-08-01
 * Time: 下午 08:38
 */
namespace Action\Operator;

use Model\UserModel;

/**
 * Class Login
 * @package Action\Operator
 * 
 * @property-read \Model\User $userModel
 */
class Login extends \Lib\Action {
    public function main() {
        $user = trim($_POST['user']);
        $pass = $_POST['password'];
        
        if(empty($user)) {
            $this->setMessage('用户名不能为空');
            $this->core->redirectToAction('login');
        }

        if(!$this->userModel->exists($user)) {
            $this->setMessage('用户不存在');
            $this->core->redirectToAction('login', ['user' => $user]);
        }
        
        if(!$this->userModel->verify($user, $pass)) {
            $this->setMessage('用户不存在');
            $this->core->redirectToAction('login', ['user' => $user]);
        }
        
        $_SESSION['logined'] = ['user' => $user];
        $this->core->redirectToAction('index');
    }
}